<template>
   
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="searchNetworkList"
          size="small"
          label-width="140px"
        >
          <el-form-item label="网点名称:">
            <el-select
              v-model="searchNetworkList.name"
              no-match-text="不存在该网点,请重新输入"
              clearable
              filterable
              :filter-method='getNetwork'
              style="width: 203px"
              placeholder="请输入要搜索的网点名称"
            >
              <el-option
                v-for="(item, index) in Networkoptions"
                :key="index"
                :label="item.name"
                :value="item.name"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="网点状态:">
            <el-select
              v-model="searchNetworkList.status"
              style="width: 203px"
              placeholder="请选择要搜索的网点状态"
              clearable
            >
              <el-option
                v-for="(item, index) in statusOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="省份:">
            <el-select
              v-model="searchNetworkList.provinceId"
              style="width: 203px"
              placeholder="请选择要搜索的省份"
              @change="showCity"
              clearable
            >
              <el-option
                v-for="(item, index) in provinceOptions"
                :key="index"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="城市:">
            <el-select
              v-model="searchNetworkList.cityId"
              style="width: 203px"
              placeholder="请选择要搜索的城市"
              @change="showArea"
              clearable
            >
              <el-option
                v-for="(item, index) in cityOptions"
                :key="index"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="地区:">
            <el-select
              v-model="searchNetworkList.areaId"
              style="width: 203px"
              placeholder="请选择要搜索的地区"
              clearable
            >
              <el-option
                v-for="(item, index) in areaOptions"
                :key="index"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="详细地址:">
            <el-input
              style="width: 203px"
              v-model="searchNetworkList.address"
              placeholder="请输入要搜索的详细地址"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card style="margin-top: 15px" class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        class="btn-add"
        style="float: right"
        @click="addDialogVisible = true"
        size="mini"
      >
        添加
      </el-button>

      <el-dialog
        title="网点添加"
        :visible.sync="addDialogVisible"
        width="60%"
        :before-close="handleClose"
      >
        <span>
          <el-card class="filter-container" shadow="never">
            <div style="margin-top: 15px">
              <el-form
                :rules="rules"
                ref="addNetwork"
                :inline="true"
                :model="addNetwork"
                size="small"
                label-width="140px"
              >

                <el-form-item prop="name" label="网点名称:">
                  <el-input
                    style="width: 203px"
                    v-model="addNetwork.name"
                    placeholder="请输入要添加的网点名称"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item prop="provinceId" label="省份:">
                  <el-select
                    v-model="addNetwork.provinceId"
                    style="width: 203px"
                    placeholder="请选择要添加的网点省份"
                    @change="showCity"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in provinceOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item prop="cityId" label="城市:">
                  <el-select
                    v-model="addNetwork.cityId"
                    style="width: 203px"
                    placeholder="请选择要添加的网点城市"
                    @change="showArea"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in cityOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item prop="areaId" label="地区:">
                  <el-select
                    v-model="addNetwork.areaId"
                    style="width: 203px"
                    placeholder="请选择要添加的网点地区"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in areaOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="详细地址:">
                  <el-input
                    style="width: 203px"
                    v-model="addNetwork.address"
                    placeholder="请输入要添加的网点详细地址"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="网点描述:">
                  <el-input
                    style="width: 203px"
                    v-model="addNetwork.desc"
                    placeholder="请输入网点描述"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAdd()">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
    <div style="margin-top: 15px" class="table-container">
      <el-table
        :data="NetworkList"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="网点ID" width="80" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="网点名称" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="网点状态" width="120" align="center">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.status == '0'">未营业</el-tag>
            <el-tag type="danger" v-else-if="scope.row.status == '-1'"
              >已倒闭</el-tag
            >
            <el-tag type="success" v-else>营业</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="网点描述" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.desc }}</template>
        </el-table-column>
        <el-table-column label="网点地址" width="275" align="center">
          <template slot-scope="scope">{{ scope.row.totalArea }}</template>
        </el-table-column>
        <el-table-column label="网点创建时间" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="
                (messageEditDialogVisible = true), showMessageData(scope.$index)
              "
              >编辑
            </el-button>
            <el-button
              size="mini"
              type="warning"
              @click="
                (statusEditDialogVisible = true),
                  showStatusData(scope.$index, scope.row)
              "
              >状态修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="网点信息编辑"
        :visible.sync="messageEditDialogVisible"
        width="60%"
        :before-close="handleClose"
      >
        <span>
          <el-card class="filter-container" shadow="never">
            <div style="margin-top: 15px">
              <el-form
                :inline="true"
                :model="editMessage"
                size="small"
                label-width="140px"
              >

                <el-form-item label="网点名称:">
                  <el-input
                    style="width: 203px"
                    v-model="editMessage.name"
                    placeholder="请输入要编辑的网点名称"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="省份:">
                  <el-select
                    v-model="editMessage.provinceId"
                    style="width: 203px"
                    placeholder="请选择要编辑的网点省份"
                    @change="showCity"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in provinceOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="城市:">
                  <el-select
                    v-model="editMessage.cityId"
                    style="width: 203px"
                    placeholder="请选择要编辑的网点城市"
                    @change="showArea"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in cityOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="地区:">
                  <el-select
                    v-model="editMessage.areaId"
                    style="width: 203px"
                    placeholder="请选择要编辑的网点地区"
                    clearable
                    @change="$forceUpdate()"
                  >
                    <el-option
                      v-for="(item, index) in areaOptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="详细地址:">
                  <el-input
                    style="width: 203px"
                    v-model="editMessage.address"
                    placeholder="请输入要编辑的网点详细地址"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="网点描述:">
                  <el-input
                    style="width: 203px"
                    v-model="editMessage.desc"
                    placeholder="请输入要编辑的网点描述"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="messageEditDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditMessage()"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <el-dialog
        title="网点状态编辑"
        :visible.sync="statusEditDialogVisible"
        width="35%"
        :before-close="handleClose"
      >
        <span>
          <el-card class="filter-container" shadow="never">
            <div style="margin-top: 15px">
              <el-form
                :inline="true"
                :model="editStatus"
                size="small"
                label-width="140px"
              >

                <el-form-item label="网点状态:">
                  <el-select
                    v-model="editStatus.status"
                    style="width: 203px"
                    placeholder="请选择网点状态"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in statusOptions"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="statusEditDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditStatus()"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :page-count="pageCunt"
        :current-page.sync="currentPage"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "brancheManage",
  data() {
    return {
      searchNetworkList: {
        pageNum: "1",
        pageSize: "5",
        status: "",
        name: "",
        provinceId: "",
        cityId: "",
        areaId: "",
        address: "",
      },
      currentPage:1,
      currentNetworkList: {
        networkId: "",
        name: "",
        type: "",
        status: "",
        phone: "",
        email: "",
        pageNum: "1",
        pageSize: "5",
      },
      NetworkList: [],
      addNetwork: {
        areaId: "",
        id: "",
        name: "",
        address: "",
        desc: "",
      },
      editMessage: {
        id: "",
        name: "",
        areaId: "",
        address: "",
        desc: "",
      },
      editStatus: {
        id: "",
        status: "",
      },
      Networkoptions: [],
      provinceOptions: [],
      cityOptions: [],
      areaOptions: [],
      statusOptions: [
        {
          value: "0",
          label: "未营业",
        },
        {
          value: "-1",
          label: "已倒闭",
        },
        {
          value: "1",
          label: "营业",
        },
      ],
      listLoading: true,
      addDialogVisible: false,
      messageEditDialogVisible: false,
      pageCunt: 1,
      statusEditDialogVisible: false,
      rules: {
        name: [{ required: true, message: "请输入网点名称", trigger: "blur" }],
        areaId: [
          { required: true, message: "请输入网点所属地区", trigger: "blur" },
        ],
        provinceId: [
          { required: true, message: "请输入网点所属省份", trigger: "blur" },
        ],
        cityId: [
          { required: true, message: "请输入网点所属城市", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      this.getProvince();
      this.axios
        .get("api/auth/network/list", {
          params: this.searchNetworkList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.NetworkList = response.data.value.data;
            this.NetworkList.forEach(function (item, index) {
              item.totalArea =
                item.area.city.province.name +
                item.area.city.name +
                item.area.name +
                item.address;
            });
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    getProvince() {
      this.axios
        .get("api/sys/province/list")
        .then((response) => {
          this.provinceOptions = response.data.value;
        })
        .catch((error) => {});
    },
    showCity(item) {
      this.searchNetworkList.cityId = "";
      this.searchNetworkList.areaId = "";
      this.editMessage.cityId = "";
      this.editMessage.areaId = "";
      this.addNetwork.areaId = "";
      this.addNetwork.cityId = "";
      this.cityOptions = [];
      this.areaOptions = [];
      if (item != 0) {
        this.axios
          .get("api/sys/city/list?provinceId=" + item)
          .then((response) => {
            this.cityOptions = response.data.value;
          })
          .catch((error) => {});
      }
    },
    showArea(item) {
      this.searchNetworkList.areaId = "";
      this.addNetwork.areaId = "";
      this.areaOptions = [];
      if (item != 0) {
        this.axios
          .get("api/sys/area/list?cityId=" + item)
          .then((response) => {
            this.areaOptions = response.data.value;
          })
          .catch((error) => {});
      }
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleResetSearch() {
      this.searchNetworkList = {
        pageNum: "1",
        pageSize: "5",
        status: "",
        name: "",
        provinceId: "",
        cityId: "",
        areaId: "",
        address: "",
      };
    },
    handleCurrentChange(val) {
      this.listLoading = true;
      this.currentNetworkList.pageNum = val;
      this.axios
        .get("api/auth/network/list", {
          params: this.currentNetworkList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.NetworkList = response.data.value.data;
            this.NetworkList.forEach(function (item, index) {
              item.totalArea =
                item.area.city.province.name +
                item.area.city.name +
                item.area.name +
                item.address;
            });
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleSearchList() {
      this.listLoading = true;
      this.axios
        .get("api/auth/network/list", {
          params: this.searchNetworkList,
        })
        .then((response) => {
          console.log(response)
          if (response.data.code == "200") {
            this.NetworkList = response.data.value.data;
            this.NetworkList.forEach(function (item, index) {
              item.totalArea =
                item.area.city.province.name +
                item.area.city.name +
                item.area.name +
                item.address;
            });
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleAdd() {
      this.$refs.addNetwork.validate((valid) => {
        if (valid) {
          this.$confirm("请再次确认是否添加")
            .then((_) => {
              this.axios
                .post(
                  "api/auth/sys/network/add?areaId=" +
                    this.addNetwork.areaId +
                    "&id=" +
                    this.addNetwork.id +
                    "&name=" +
                    this.addNetwork.name +
                    "&address=" +
                    this.addNetwork.address +
                    "&desc=" +
                    this.addNetwork.desc
                )
                .then((response) => {
                  if (response.data.code == "200") {
                    this.$message({
                      message: "添加成功",
                      type: "success",
                    });
                    this.getList();
                    this.addDialogVisible = false;
                  } else {
                    this.$message.error("添加失败");
                  }
                })
                .catch((error) => {
                  this.$message.error(response.data.message);
                });
            })
            .catch((err) => {
              this.$message.error(err);
            });
        } else {
          this.$message.error("请填写完将带*号的选项");
        }
      });
    },
    showMessageData(index) {
      this.editMessage = {...this.NetworkList[index]};
      this.editMessage.provinceId =
        this.NetworkList[index].area.city.province.id;
      this.showCity(this.editMessage.provinceId);
      this.editMessage.cityId = this.NetworkList[index].area.city.id;
      this.showArea(this.editMessage.cityId);
      this.editMessage.areaId = this.NetworkList[index].area.id;
    },
    handleEditMessage() {
      this.$confirm("请再次确认是否选择修改")
        .then((_) => {
          this.axios
            .post(
              "api/auth/sys/network/update?id=" +
                this.editMessage.id +
                "&name=" +
                this.editMessage.name +
                "&areaId=" +
                this.editMessage.areaId +
                "&address=" +
                this.editMessage.address +
                "&desc=" +
                this.editMessage.desc
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.messageEditDialogVisible = false;
                this.getList()
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("修改失败");
            });
        })
        .catch((error) => {
          this.$message.error("修改失败");
        });
    },
    showStatusData(index) {
      this.editStatus = {...this.NetworkList[index]};
    },
    handleEditStatus() {
      this.$confirm("请再次确认是否选择修改")
        .then((_) => {
          this.axios
            .post(
              "api/auth/sys/network/change?id=" +
                this.editStatus.id +
                "&status=" +
                this.editStatus.status
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.statusEditDialogVisible = false;
                this.getList()
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("修改失败");
            });
        })
        .catch((_) => {});
    },
    getNetwork(val) {
      if (val != "") {
        this.axios
          .get(
            "api/sys/network/search?name="+val
          )
          .then((response) => {
            console.log(response)
            if (response.data.code == "200") {
              this.Networkoptions = response.data.value
            } else {
              this.$message.error("未获取到网点数据");
            }
          })
          .catch((error) => {
            this.$message.error(response.data.message);
          });
      }else{
        this.Networkoptions = []
      }
    },
  },
};
</script>
<style></style>


